package com.blog4j.contentVerify.processor;

import com.aliyun.green20220302.Client;
import com.aliyun.teaopenapi.models.Config;
import com.blog4j.contentVerify.constants.VerifyConstants;
import com.blog4j.contentVerify.context.ContentVerifyContext;
import com.blog4j.contentVerify.enums.TextServiceEnum;
import com.blog4j.contentVerify.enums.VerifyTypeEnum;
import com.blog4j.contentVerify.exception.ContentVerifyException;
import com.blog4j.contentVerify.model.ImageVerifyModel;
import com.blog4j.contentVerify.model.TextVerifyModel;
import com.blog4j.contentVerify.properties.AliyunAccessProperties;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Objects;
import java.util.Optional;

/**
 * @author 98k灬
 * @version v1.0.0
 * @Description : 功能描述
 * @Create on : 2024/9/1 19:41
 **/
@Slf4j
public abstract class AbstractVerifyProcessor implements VerifyProcessor {

    private static final String END_POINT = "green-cip.cn-shanghai.aliyuncs.com";

    private AliyunAccessProperties aliyunAccessProperties;

    @Autowired
    public void setAliyunAccessProperties(AliyunAccessProperties aliyunAccessProperties) {
        this.aliyunAccessProperties = aliyunAccessProperties;
    }

    /**
     * 内容安全审核
     *
     * @param verifyContext 审核上下文信息
     */
    @Override
    public void contentVerify(ContentVerifyContext verifyContext) {
        validation(verifyContext);
        VerifyTypeEnum verifyType = verifyContext.getVerifyType();
        if (VerifyTypeEnum.TEXT == verifyType) {
            doTextVerify(verifyContext);
            return;
        }
        doImageVerify(verifyContext);
    }

    protected abstract void doTextVerify(ContentVerifyContext verifyContext);

    protected abstract void doImageVerify(ContentVerifyContext verifyContext);

    private void validation(ContentVerifyContext verifyContext) {
        VerifyTypeEnum verifyType = verifyContext.getVerifyType();
        if (Objects.isNull(verifyType)) {
            throw new ContentVerifyException("审核类型不能为空");
        }

        if (verifyType == VerifyTypeEnum.TEXT) {
            checkTextVerifyModel(verifyContext);
        } else if (verifyType == VerifyTypeEnum.IMAGE) {
            checkImageVerifyModel(verifyContext);
        } else if (verifyType == VerifyTypeEnum.URL) {
            checkUrlVerifyModel(verifyContext);
        } else {
            throw new ContentVerifyException("非法的审核类型");
        }

        Config config = new Config().setAccessKeyId(aliyunAccessProperties.getAccessKey())
                .setAccessKeySecret(aliyunAccessProperties.getAccessSecret());
        config.endpoint = END_POINT;
        Client client;
        try {
            client = new Client(config);
        } catch (Exception exception) {
            throw new ContentVerifyException(exception.getMessage());
        }
        verifyContext.setClient(client);
    }

    private void checkUrlVerifyModel(ContentVerifyContext verifyContext) {
    }

    private void checkImageVerifyModel(ContentVerifyContext verifyContext) {
        ImageVerifyModel imageVerifyModel = Optional.ofNullable(verifyContext.getImageVerifyModel()).orElseThrow(() ->
                new ContentVerifyException("审核的图片URL不能为空"));

        String imageUrl = imageVerifyModel.getImageUrl();
        if (StringUtils.isBlank(imageUrl)) {
            throw new ContentVerifyException("审核的图片URL不能为空");
        }

        if (imageUrl.length() > VerifyConstants.IMAGE_URL_LENGTH) {
            throw new ContentVerifyException("审核的图片URL长度过长");
        }
    }

    private void checkTextVerifyModel(ContentVerifyContext verifyContext) {
        TextVerifyModel textVerifyModel = Optional.ofNullable(verifyContext.getTextVerifyModel()).orElseThrow(() ->
                new ContentVerifyException("审核的文本内容不能为空"));
        String textContent = textVerifyModel.getTextContent();
        if (StringUtils.isBlank(textContent)) {
            throw new ContentVerifyException("审核的文本内容不能为空");
        }

        if (textContent.length() > VerifyConstants.TEXT_LENGTH) {
            throw new ContentVerifyException("待审核的文本内容超出上限");
        }

        String textService = textVerifyModel.getTextService();
        if (StringUtils.isBlank(textService)) {
            throw new ContentVerifyException("文本审核类型不能为空");
        }

        if (!StringUtils.equals(textService, TextServiceEnum.CHAT_DETECTION.getService()) &&
                !StringUtils.equals(textService, TextServiceEnum.NICKNAME_DETECTION.getService()) &&
                !StringUtils.equals(textService, TextServiceEnum.COMMENT_DETECTION.getService())) {
            throw new ContentVerifyException("非法的文本审核类型");
        }
    }
}
